// 创建expess服务
const express = require('express')
const dayjs = require('dayjs')
const connection = require('./db')
const { port } = require('./config')
const router = require('./routers/user')
const app = express()


let bodyParser = require('body-parser')

// let objMulter = multer({storage: storage})



app.set('view engine','ejs')
app.set('views', __dirname + '/views'); 

app.use(bodyParser.json({limit: '1mb'}));  //body-parser 解析json格式数据
app.use(bodyParser.urlencoded({            //此项必须在 bodyParser.json 下面,为参数编码
  extended: true
}));


// 加载首页
app.get('/',router.users)
// 编辑回显用户
app.get('/editUsers/:id',router.editUsers)
// 编辑更新用户
app.post('/updateAdd',router.updateAdd)
// 删除用户
app.get('/deleteUsers/:id',router.deleteUsers)
// 取消
app.get('/cancel',router.cancel)
// 用户新增页面
app.get('/userAddPage',router.userAddPage)

// 用户新增
app.get('/userAdd',router.userAddPage)

// 文件上传跳转
app.get('/upload',router.upload)




// 文件上传的提交
let multer = require('multer')
// 文件的存储路径
//var upload = multer({ dest: 'upload/'});

var storage = multer.diskStorage({
  destination: function (req, file, cb) {
    cb(null, 'upload/')
  },
  filename: function (req, file, cb) {
    console.log('file',file)
    cb(null, file.originalname)
  }
})

var upload = multer({ storage: storage })


// 类型定义
var type = upload.single('editormd-image-file')
// 接口调用
app.post('/gists',type,router.uploadFile)





app.listen(port,()=>{
  console.log(`${port}端口监听中`)
})